// pages/operation/teacher/teacherDetail/index.js
import { TeacherModel } from "../../../../api/api-work/teacherModel.js";
const teacherModel = new TeacherModel();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "", // 老师id
    activeTab: "first",
    nickName: "", // 姓名
    genderName: "", // 性别
    mobile: "", // 手机号
    gender: "",
    // 当前授课班级
    classPageNum: 1,
    classPageSize: 10,
    classTotal: 0,
    classTableDatas: [],
    classLoadStatus: "",
    classtriggered: false,
    showClass: false, // 课程弹出
    showClassDatas: [], // 课程弹出数组
    // 学员
    studentPageNum: 1,
    studentPageSize: 10,
    studentTotal: 0,
    studentTableDatas: [],
    studentLoadStatus: "",
    studenttriggered: false,
    mobileShow: false, // 电话弹出层
    mobileShowDatas: [], // 电话号数组
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // 设置导航title
    wx.setNavigationBarTitle({
      title: options.nickName
    })
    this.setData({
      id: options.id
    })

    // 获取老师基本信息
    await this.getTeacherInfos()
    // await this.getTeacherClasses()
  },

  // 获取老师基本信息
  async getTeacherInfos() {
    const result = await teacherModel.getTeacherInfoApi({ id: this.data.id })
    this.setData({
      nickName: result.nickName,
      genderName: result.genderName,
      gender: result.gender,
      mobile: result.mobile
    })
  },

  // 获取当前授课班级
  async getTeacherClasses() {
    const { total, rows } = await teacherModel.getTeacherClassApi({ teacherId: this.data.id, pageNum: this.data.classPageNum, pageSize: 15 })
    let arr = this.data.classTableDatas;
    arr.push(...rows);
    this.setData({
      classTableDatas: arr,
      classTotal: total
    })
    if (this.data.classTotal == 0) {
      this.setData({
        // 无数据
        classLoadStatus: ""
      })
    }
    else if (this.data.classTableDatas.length < this.data.classTotal) {
      this.setData({
        // 加载更多
        classLoadStatus: "loadmore"
      })
    }
    else {
      // 没有数据了
      this.setData({
        classLoadStatus: "nomore"
      })
    }
  },

  // 当前授课班级上拉加载
  getMoreClass() {
    if (this.data.classLoadStatus === "nomore") {
      // 没有更多了
      return;
    }
    this.setData({
      classPageNum: this.data.classPageNum + 1,
      classLoadStatus: "loading"
    })
    this.getTeacherClasses()
  },

  // 当前授课班级下拉刷新
  refreshMoreClass() {
    this.setData({
      classtriggered: false,
      classPageNum: 1,
      classTableDatas: []
    })
    this.getTeacherClasses()
  },
  // 点击课程
  clickCourseName(e) {
    const item = e.currentTarget.dataset.query;
    if (!item.courseName) {
      return
    }
    else {
      const arr = item.courseName.split(",");
      if (arr.length > 1) {
        this.setData({
          showClass: true,
          showClassDatas: arr
        })
      }
    }
  },
  // 关闭课程弹出
  onCloseClass() {
    this.setData({
      showClass: false,
      showClassDatas: []
    })
  },
  // 点击手机号
  clickMobile(e) {
    const item = e.currentTarget.dataset.query;
    // const arr = 
    this.setData({
      mobileShow: true,
      mobileShowDatas: item.familyList
    })
  },
  // 拨打电话
  callNumber(e) {
    const item = e.currentTarget.dataset.query;
    wx.makePhoneCall({
      phoneNumber: item.mobile,
      //成功回调
      success(res) {
        console.log("成功：", res);
      },
      //失败回调
      fail(res) {
        console.log("失败：", res);
      },
      complete(res) {
        this.setData({
          mobileShow: false,
        })
      },
    })
  },
  onCloseMobile() {
    this.setData({
      mobileShow: false,
    })
  },

  // 获取学员信息
  async getTeacherStudents() {
    const { total, rows } = await teacherModel.getTeacherStudentsApi({ teacherId: this.data.id, pageNum: this.data.studentPageNum, pageSize: 15 })
    let arr = this.data.studentTableDatas;
    arr.push(...rows);
    this.setData({
      studentTableDatas: arr,
      studentTotal: total
    })
    if (this.data.studentTotal == 0) {
      this.setData({
        studentLoadStatus: ""
      })
    }
    else if (this.data.studentTableDatas.length < this.data.studentTotal) {
      this.setData({
        // 加载更多
        studentLoadStatus: "loadmore"
      })
    }
    else {
      this.setData({
        studentLoadStatus: "nomore"
      })
    }
  },
  // 学员上拉加载
  getMoreStudent() {
    if (this.data.studentLoadStatus === "nomore") {
      return
    }
    this.setData({
      studentPageNum: this.data.studentPageNum + 1,
      studentLoadStatus: "loading"
    })
    this.getTeacherStudents()
  },

  // 学员下拉刷新
  refreshMoreStudent() {
    this.setData({
      studenttriggered: false,
      studentPageNum: 1,
      studentTableDatas: []
    })
    this.getTeacherStudents()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  // tab切换
  onTabChange(e) {
    this.setData({
      activeTab: e.detail.name
    })
    if (e.detail.name == "first") {
      // 基本信息
      this.getTeacherInfos()
    } else if (e.detail.name == "second") {
      // 当前授课班级
      this.setData({
        classPageNum: 1,
        classTableDatas: []
      })
      this.getTeacherClasses();
    } else {
      // 学员
      this.setData({
        studentPageNum: 1,
        studentTableDatas: []
      })
      this.getTeacherStudents();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})